﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using HigLabo.Net.Extensions;
using Newtonsoft.Json.Linq;

namespace HigLabo.Net.Twitter
{
    /// <summary>
    /// 
    /// </summary>
    public class Friendship : ResponseObject
    {
        private Int64 _ID = 0;
        private String _IDStr = "";
        private String _Name = "";
        private String _ScreenName = "";
        private Boolean _Following = false;
        private Boolean _FollowedBy = false;
        private Boolean _FollowingRequest = false;
        /// <summary>
        /// 
        /// </summary>
        public Int64 ID
        {
            get { return _ID; }
            set { _ID = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public String IDStr
        {
            get { return _IDStr; }
            set { _IDStr = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public String Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public String ScreenName
        {
            get { return _ScreenName; }
            set { _ScreenName = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public Boolean Following
        {
            get { return _Following; }
            set { _Following = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public Boolean FollowedBy
        {
            get { return _FollowedBy; }
            set { _FollowedBy = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public Boolean FollowingRequest
        {
            get { return _FollowingRequest; }
            set { _FollowingRequest = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="jsonText"></param>
        public Friendship(String jsonText)
        {
            this.SetProperty(jsonText);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="jsonText"></param>
        public override void SetProperty(string jsonText)
        {
            var d = this.SetData(jsonText);

            object objOut = null;

            this.ID = d.ToInt64("id") ?? this.ID;
            this.Name = d.ToString("name");
            this.ScreenName = d.ToString("screen_name");
            this.IDStr = d.ToString("id_str");

            d.TryGetValue("connections", out objOut);
            JContainer val = (JContainer)objOut;

            for (int i = 0; i < val.Count(); i++)
            {
                if (val.ElementAt(i).ToString() == "following")
                {
                    this.Following = true;
                }
                if (val.ElementAt(i).ToString() == "followed_by")
                {
                    this.FollowedBy = true;
                }
                if (val.ElementAt(i).ToString() == "following_requested")
                {
                    this.FollowingRequest = true;
                }
            }            
        }
    }
}
